package com.cep.client;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.TextView;

public class NameWatcher implements TextWatcher {
	private static NameWatcher instance = null;

	public static void init(TextView consoleOutputTV_,
			CEPAndroidClientActivity parent_) {
		instance = new NameWatcher(consoleOutputTV_, parent_);
	}

	public static NameWatcher instance() {
		return instance;
	}

	private String name = null;
	private TextView consoleOutputTV = null;
	private CEPAndroidClientActivity parent = null;

	private NameWatcher(TextView consoleOutputTV_,
			CEPAndroidClientActivity parent_) {
		this.consoleOutputTV = consoleOutputTV_;
		name = "NAME";
		parent = parent_;
	}

	@Override
	public void afterTextChanged(Editable arg0) {
		this.name = arg0.toString();
		if (checkName(name) == false) {
			consoleOutputTV.setText("Name is too short");
			parent.enableInput(false);
		} else {
			consoleOutputTV.setText("");
			parent.enableInput(true);
		}
	}

	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
	}

	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

	}

	public String getName() {
		return name;
	}

	private boolean checkName(String name) {
		return name.length() >= 2;
	}
}
